package com.sq.partner.manager.controller;

import com.sq.partner.manager.service.AntPolicyAndClaimService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.SqlUtil;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("policyAndClaimController")
public class AntPolicyAndClaimController {
    @Resource
    AntPolicyAndClaimService antPolicyAndClaimService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(){
        return "mayiAdj/policyAndClaim";
    }

    @RequestMapping(value = "/queryPolicyAndClaim",method = RequestMethod.POST)
    @ResponseBody
    public Page queryPolicyAndClaimByMap(Integer pageIndex,String flag, String rgstDptCde) throws Exception{
        Page page1=checkBeforeQuery(pageIndex,flag, rgstDptCde);
        return  antPolicyAndClaimService.queryPolicyAndClaim(page1);
    }

    public Page checkBeforeQuery(Integer pageIndex, String flag, String rgstDptCde) throws Exception{
        Page page = new Page();
        Map<String,Object> map = new HashMap<String, Object>();
        if(pageIndex != -1){
            page.setCurrent(pageIndex);
            map = SqlUtil.buildPageMap(page);
        }
        map.put("flag",flag);
		map.put("rgstDptCde", rgstDptCde);
        page.setType(map);
        return page;
    }
    
    
    @SuppressWarnings("resource")
	@RequestMapping(value = "exportExcel")
	@ResponseBody
	public void exportExcel(HttpServletResponse response, HttpServletRequest request, String flag) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("flag", flag);
		//查数据
		List<Map<String,Object>> policyList = antPolicyAndClaimService.queryPolicyByCondition(map);
		List<String[]> tempList = new ArrayList<String[]>();
		String fileName = "";
		String sheetName = "";
	
		HSSFWorkbook wb = new HSSFWorkbook();
		if("1".equals(flag)){
			fileName = "蚂蚁保单信息表";
			sheetName = "蚂蚁保单信息信息";
			int columnNumber = 8;
			// 列宽
			int[] columnWidth = {5, 15, 15, 25, 10, 10, 20, 20, };
			// 列名
			String[] columnName = {"序号", "保单号", "起保日期", "终保日期", "保费", "保额", "交易时间", "推送状态"};
			// 序号
			int count = 1;
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
			for (Map<String,Object> p : policyList) {
				tempList.add(new String[]{
						 String.valueOf(count++), 
						 (String) p.get("POLICY_NO"),
						 p.get("INSURE_START") == null ? "":df.format(p.get("INSURE_START")), 
						 p.get("INSURE_END") == null ? "":df.format(p.get("INSURE_END")), 
						 String.valueOf(p.get("PREMIUM")) ,
						 String.valueOf(p.get("AMT")) ,
						 p.get("TRADE_DATE")==null?"":df.format(p.get("PAY_TIME"))
						  , (String) p.get("PSTATE")});
			}
			String[][] infos = tempList.toArray(new String[][]{});
			wb = antPolicyAndClaimService.generateHssWorkBook(sheetName, columnNumber, columnWidth, columnName, infos);
		}else {
			fileName = "蚂蚁理赔信息表";
			sheetName = "蚂蚁理赔信息";
			// 列数
			int columnNumber = 6;
			// 列宽
			int[] columnWidth = {5, 15, 15, 25, 10, 10,  };
			// 列名
			String[] columnName = {"序号", "大赔案号", "大保单号", "推送状态", "理赔金额", "交易时间"};
			// 序号
			int count = 1;
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
			for (Map<String,Object> p : policyList) {
				tempList.add(new String[]{String.valueOf(count++), 
						(String) p.get("RELA_REPORT_NO"), 
						(String) p.get("POLICY_NO2"), 
						(String) p.get("CSTATE"),
						String.valueOf(p.get("CLAIM_AMOUNT")) ,
						(String)p.get("TRADE_DATE")});
			}
			String[][] infos = tempList.toArray(new String[][]{});
			wb = antPolicyAndClaimService.generateHssWorkBook(sheetName, columnNumber, columnWidth, columnName, infos);
		}
		
		
		response.setContentType("application/ms-excel;charset=utf-8");
		String userAgent = request.getHeader("user-agent").toLowerCase();
		if (userAgent.contains("msie") || userAgent.contains("trident") || userAgent.contains("edge")) {
			// IE浏览器
			fileName = URLEncoder.encode(fileName, "UTF-8");
		} else {
			// 非IE浏览器
			fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
		}
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
		OutputStream os = response.getOutputStream();
		try {
			wb.write(os);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			os.close();
		}
	}
}
